﻿using H.Logs;
using System;

namespace H.Logger.Logs.Listeners
{
    /// <summary>
    /// 本地日志监听器(日志将会输出到控制台)
    /// </summary>
    public class HLocalLogListener : ILogListener
    {
        /// <summary>
        /// 缓存本地监听器实例
        /// </summary>
        private static WeakReference _Instance;

        /// <summary>
        /// 获取本地日志监听器实例
        /// </summary>
        /// <returns></returns>
        public static ILogListener GetInstance()
        {
            if (_Instance == null || !_Instance.IsAlive)
            {
                _Instance = new WeakReference(new HLocalLogListener());
            }
            return _Instance.Target as ILogListener;
        }

        private HLocalLogListener()
        {

        }

        ///<inheritdoc/>
        public void Write(string str)
        {
            Console.Write(str);
        }

        ///<inheritdoc/>
        private void Dispose(bool disposing)
        {
            if (disposing) { }
        }

        ///<inheritdoc/>
        public void Dispose()
        {
            Dispose(true);
        }

        ~HLocalLogListener()
        {
            Dispose(false);
        }
    }
}
